<?php
$link = mysql_connect('localhost', 'root', '');
//$link = mysql_connect('aecabill.accountsupportmysql.com', 'aeca', 'fArC5YyyG8NT9wdC5LXP');
if (!$link) { 
     die('There appeared to be an error while connecting to the website database: ' . mysql_error()); 
}
mysql_select_db(aeca);

//include ("includes/gen_password.php");
//$user_id = $_GET['Id'];
$user_id = $_POST['uid'];

$query = "select * from tbl_reporting_personnel where rp_id=$user_id";
$result = mysql_fetch_array(mysql_query($query));

$cquery = "select * from  tbl_companies";
$cresult = mysql_query($cquery);

if($_POST['submit'] == 'Submit')
{
	extract($_POST);
	$update_query = "UPDATE tbl_reporting_personnel SET rp_first_name='$rp_first_name',rp_last_name='$rp_last_name',c_nickname='$c_nickname',rp_phone_number='$rp_phone_number'," .
					"rp_phone_ext='$rp_phone_ext',rp_fax_number='$rp_fax_number',rp_email='$rp_email',rp_username='$rp_user_name' WHERE rp_id = '".$user_id."' LIMIT 1;";
	if(mysql_query($update_query))
	{?>
		<script>
				$(document).ready(function(){
					 $("#success").text("Updated Successfully!").show();
					 return false;
				});
				</script>
	<?}
	$query = "select * from tbl_reporting_personnel where rp_id=$user_id";
$result = mysql_fetch_array(mysql_query($query));
	
}else if($_POST['reset'] == 'Reset Password'){
	$newPass = generatePassword();
	$newmd5Pass = md5($newPass);
    $sql = "SELECT * from tbl_reporting_personnel WHERE rp_id  =$user_id"; 
    $result = mysql_query($sql);
    $row = mysql_fetch_assoc($result);
    if (!$result) {die('Query error: ' . mysql_error());}                
    if (mysql_num_rows($result)!= 1) {$error = 1;} else {
	    	$email = $row['rp_email'];
    }
    $up_sql = "UPDATE tbl_reporting_personnel SET rp_password = '".$newmd5Pass."' WHERE rp_id = $user_id LIMIT 1;";
    $up_result = mysql_query($up_sql);                  
    if (!$up_result) {die('Query error: '. mysql_error());}
    $subject = "AECA Reporting Portal";
    $body = "The password for the AECA Reporting Portal user account with a user name of: ".$row['rp_username']." has been reset to the password shown below:"."\n".$newPass."\n"."\n"."Please contact AECA if you continue to experience problems logging into your account.";
    if (mail($email, $subject, $body)) {$success = 1;} else {$form = "nosend"; $success = 0;}
    if($success == 1){?>
    <script>
	$(document).ready(function(){
	 	$("#success").text("Updated Successfully!").show();
		return false;
	});
	</script>
    <?}elseif($success == 0){?>
    <script>
	$(document).ready(function(){
	 	$("#error").text("There was an error while attempting to reset your password. Please contact AECA.").show();
		return false;
	});
	</script>
    <?}
}

?>
<?if($_POST['reset']){?>
<meta http-equiv=refresh
content="text/html; index.php?page=edit-reporting-personnel&Id=<?=$user_id?>">
<?}?>
<span id='error'></span><span id='success'></span>
<script>
  $(document).ready(function(){
    $("#personal_info").validate();
  });
</script>
<script>
$(function() {
	$( "#accordion" ).accordion();
});
</script>
<div id="accordion">
<h3><a href="#">Edit Reporting Personnel</a></h3>
<div>
<form action="index.php?page=edit-reporting-personnel&Id=<?=$user_id?>" method="post" id="personal_info">
<table cellpadding="4">
	<tr>
		<td><label><strong>First Name :</strong></label></td>
		<td><input type="text" name="rp_first_name" value="<?=$result[2];?>" /></td>
	</tr>
	<tr>
		<td><label><strong>Last Name :</strong></label></td>
		<td><input type="text" name="rp_last_name" value="<?=$result[3];?>"/></td>
	</tr>
	<tr>
		<td><label><strong>Company Name :</strong></label></td>
		<? if($_SESSION['user_level'] == 4){?>	
		<td>
		<select name="c_nickname">
		<? while($crow = mysql_fetch_array($cresult)){?>
										<option value=<?=$crow['c_nickname'];?>3221
										<? if($result[1] == $crow['c_nickname']){ echo 'selected="selected"'; } else {} ?>
										><?=$crow['c_nickname'];?></option>
		<?}?>
		</select>
		</td>
		<?}else{?>
		<td>
			<input type="text" name="c_nickname" value="<?=$result[1];?>123" class="number"/>
		</td>
		<?}?>
	</tr>
	<tr>
		<td><label><strong>Phone Number :</strong></label></td>
		<td><input type="text" name="rp_phone_number" value="<?=$result[4];?>" class="number"/></td>
	</tr>
	<tr>
		<td><label><strong>Extension :</strong></label></td>
		<td><input type="text" name="rp_phone_ext" value="<? echo $result[5];?>" class="number"/></td>
	</tr>
	<tr>
		<td><label><strong>Fax Number :</strong></label></td>
		<td><input type="text" name="rp_fax_number" value="<? echo $result[6];?>" class="number"/></td>
	</tr>
	<tr>
		<td><label><strong>Email :</strong></label></td>
		<td><input type="text" name="rp_email" value="<? echo $result[7];?>" class="required email"/></td>
	</tr>
	<? if($_SESSION['user_level'] == 4){?>	
	<tr>
		<td><label><strong>Username* :</strong></label></td>
		<td><input type="text" name="rp_user_name" value="<? echo $result[8];?>" class="required"/></td>
	</tr>
	<? }else{?>
	<tr>
		<td><label><strong>Username :</strong></label></td>
		<td><input type="text" name="rp_user_name" value="<? echo $result[8];?>" readonly/></td>
	</tr>
	<? }?>
	<tr>
		<td>&nbsp;</td>
		<td><input type="submit" value="Submit" name="submit" /></td>
	</tr>
</table>
</form>
</div>
	<h3><a href="#">Reset Password</a></h3>
	<div>
	<form method="POST" action="index.php?page=edit-reporting-personnel&Id=<?=$user_id?>">
		<table>
			<tr>
				<td>Username:</td>
				<td><?=$result[8];?><input type="hidden" name="userid" value="<?=$user_id?>" /></td>
			</tr>
			<tr>
				<td>Password:</td>
				<td><input type="submit"  name="reset" value="Reset Password" /></td>
			</tr>
		</table>
	</form>
	</div>
</div>